﻿import QtQuick 2.0

Rectangle{
    id: shotsRoto;
    property Flickable flick: null;
    property Item gameCore: null;
    signal setRolePosition(var xRatio,var yRatio);
    
    color: "transparent";
    border.color: "black";
    border.width: 1;
    
    function updateIndicatorPosition(){
        indicator.updatePosition();
    }
    
    Connections{
        target: flick;
        onContentXChanged: Qt.callLater(indicator.updatePosition);
        onContentYChanged: Qt.callLater(indicator.updatePosition);
    }
    
    MouseArea{
        anchors.fill: parent;
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onPressed: {
            mouse.accepted = true;
            if( mouse.button === Qt.LeftButton ){
                var cX = mouseX / width * flick.contentWidth - flick.width/2;
                var cY = mouseY / height * flick.contentHeight - flick.height/2;
                flick.contentX = Math.max(0,cX);
                flick.contentY = Math.max(0,cY);
            }
            else if( mouse.button === Qt.RightButton ){
                setRolePosition(mouseX/width,mouseY/height);
            }
        }
    }
    
    Loader{
        anchors.fill: parent;
        asynchronous: true;
        sourceComponent:ShaderEffectSource{
            live: true;
            width: shotsRoto.width;
            height: shotsRoto.height;
            format: ShaderEffectSource.RGBA;
            sourceItem: gameCore;
        }
    }
    
    Rectangle{
        id: indicator;
        property int minMargin: 0;
        width: flick.width/flick.contentWidth*parent.width;
        height: flick.height/flick.contentHeight*parent.height;
        color: "transparent";
        border.color: "black";
        border.width: 2;
        
        Drag.active: dragArea.drag.active;
        Drag.hotSpot.x: 10
        Drag.hotSpot.y: 10
        
        function updatePosition(){
            if( dragArea.pressed )
                return;
            x = flick.contentX/flick.contentWidth * parent.width;
            y = flick.contentY/flick.contentHeight * parent.height;
        }
        
        MouseArea {
            id: dragArea
            anchors.fill: parent;
            drag.target: parent;
            onPressed: mouse.accepted = true;
            onWheel: mouse.accepted = true;
        }
        
        onXChanged: {
            if( x+width >= parent.width-minMargin )
                x = parent.width - width - minMargin;
            else if( x <= minMargin )
                x = minMargin;
            flick.contentX = x / parent.width * flick.contentWidth;
        }
        
        onYChanged: {
            if( y+height >= parent.height-minMargin )
                y = parent.height - height -minMargin;
            else if( y <=minMargin )
                y = minMargin;
            flick.contentY = y / parent.height * flick.contentHeight;
        }
    }
    
}
